package Thread;

public class SycDemo2 {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Thread t1 = new Thread("海帆"){
            @Override
            public void run() {
                shop.buy();
            }
        };

        Thread t2 = new Thread("青玉"){
            @Override
            public void run() {
                shop.buy();
            }
        };

        t1.start();
        t2.start();
    }
}

class Shop{
    //public synchronized void buy(){
    public void buy(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":"+"在挑衣服");
            Thread.sleep(1000);
            synchronized (this){
            //synchronized (new Object()){//使用new时一定无效
            System.out.println(t.getName()+":"+"在试衣服");
            Thread.sleep(1000);
            }
            System.out.println("结账了");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
